package com.psm.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.psm.interceptor.IndexInterceptor;
import com.psm.model.DataGrid;
import com.psm.model.WXReply;
/**
 * 
 * @author 广州朴石茗文化发展有限公司 @Email pushiming@sina.cn
 * Aug 1, 2015 
 * TODO
 */
@Before(IndexInterceptor.class)
public class WXReplyController extends BaseController<WXReply>{

	public Logger log=Logger.getLogger(getClass());
	/**
	 * 主页
	 */
    public void index(){
        render("index.html");
    }
    
    public void aut_reply(){
        render("index.html");
    }
    
    /**
     * 列表数据
     */
    public void listJson(){

        List<Object> param = new ArrayList<Object>();
 
        Page<Record> p = Db.paginate(this.getParaToInt("page", 1), this.getParaToInt("rows", 20), "select t.* ", "from " + WXReply.tableName + " t  where 1=1 ", param.toArray());
        DataGrid dg = new DataGrid();
        dg.setRows(p.getList());
        dg.setTotal(p.getTotalRow());

        renderJson(dg);
    }
    
    /**
     * 编辑、添加页
     */
    public void edit() {
        try {
            Long id = getParaToLong("id");
            
            WXReply wx = WXReply.wx.findById(id);;
            this.setAttr("po",wx);
        } catch (Exception e) {
            log.error("编辑异常", e);
        }
    }
    
    /**
     * 保存信息
     */
    public void save(){
    	/**
    	 * 获取数据
    	 */
    	 String id = getPara("id","");
    	 String replytype = getPara("reply_type","");
         String msgtype = getPara("msg_type","");
         String content = getPara("content","");
         String status = getPara("status","");
         String key_word = getPara("key_word","");
         /**
          * 新建对象
          */
         WXReply wx = null;
         if("".equals(id)){
             wx = new WXReply();
         }else{
             wx = WXReply.wx.findById(id);
         }
         if(replytype.equals("701")){
        	 wx.set("key_word","system");
         }else {
        	 wx.set("key_word",key_word);
		}
         wx.set("reply_type",replytype);
         wx.set("msg_type",msgtype);
         wx.set("content",content);
         wx.set("status",status);
         /**
          * 更新数据
          */
         if("".equals(id)){
        	 wx.set("create_time", System.currentTimeMillis()/1000);
             wx.save();
         }else{
        	 wx.set("modify_time", System.currentTimeMillis()/1000);
             wx.update();
         }
         this.rendJson_(1, "操作成功");
    }
}
